package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"strings"
)

//可以访问的域名
var (
	DomainLists = make(map[int]string)
)

func Domain() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		origin := ctx.Request.Header.Get("Origin")
		fmt.Println("--->", origin)
		fmt.Println(DomainLists)
		id, b := isok(origin)
		if !b {
			ctx.AbortWithStatusJSON(200, gin.H{"message": "斤斤计较军军军军军军"})
			ctx.Abort()
		} else {
			fmt.Println("RoleID", id)
			ctx.Set("RoleID", uint(id)) //其实就是公司ID
		}
	}
}

func isok(origin string) (id int, b bool) {
	if len(origin) == 0 {
		return 0, false
	}
	for k, v := range DomainLists {
		if strings.Contains(origin, v) {
			return k, true
		}
	}
	return 0, b
}

func init() {
	DomainLists[-1] = "127.0.0.1"
	DomainLists[-2] = "localhost"
}
